package com.marvels.financial.loan.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.DateUtils;
import com.marvels.financial.funds.dto.FundDTO;
import com.marvels.financial.loan.api.FinancialFundSourceApi;
import com.marvels.financial.loan.api.FinancialRepStyleApi;
import com.marvels.financial.consum.FinancialConsumerCoreApi;
import com.marvels.financial.consum.FinancialConsumerProductApi;
import com.marvels.financial.loan.dao.McfePaymentWithholdDetailDao;
import com.marvels.financial.loan.dto.CascadeChannelDto;
import com.marvels.financial.loan.dto.McptChannelDto;
import com.marvels.financial.loan.dto.McptProductType;
import com.marvels.financial.loan.dto.PaymentFlowConditionDTO;
import com.marvels.financial.loan.dto.PaymentFlowDetailDTO;
import com.marvels.financial.loan.dto.TrialCostConditionDTO;
import com.marvels.financial.loan.dto.TrialCostDataDTO;
import com.marvels.financial.loan.service.BalanceAccountApi;

@Service
public class BalanceAccountImpl implements BalanceAccountApi{

	@Autowired
	private McfePaymentWithholdDetailDao detailDao;
	
	@Autowired
	private FinancialConsumerProductApi consumerProductApi;
	
	@Autowired
	private FinancialFundSourceApi financialFundSourceApi;
	

	@Autowired
	private FinancialRepStyleApi financialRepStyleApi;
	
	@Autowired
	private FinancialConsumerCoreApi consumerCoreApi;
	
	@Override
	public Map<String, Object> listScreenCondition() throws MarvelsBusinessException{
		Map<String, Object> map = new HashMap<>();
		List<FundDTO> fundSourceList = financialFundSourceApi.fundSourceInfo(new FundDTO());
		McptChannelDto mcptChannelDto=new McptChannelDto();
		CascadeChannelDto channelResult = consumerProductApi.mcptChannelInfo(mcptChannelDto);
		List<McptProductType> productList = consumerProductApi.mcptProductTypeInfo(new McptProductType());
		map.put("productList", productList);
		map.put("channelList", channelResult);
		map.put("fundList", fundSourceList);
		map.put("payStyleList", financialRepStyleApi.selectAllStyle());
		return map;
	}
	
	@Override
	public Map<String, Object> listQuery(PaymentFlowConditionDTO conditionDTO)  throws MarvelsBusinessException{
		int pageSize = conditionDTO.getPageSize();
		int pageNum = conditionDTO.getPageNum();
		if (pageSize < 1) {
			pageSize = PageConstant.PAGESIZE;
		}
		if (pageNum < 1) {
			pageNum = PageConstant.CURRPAGE;
		}
		PageHelper.startPage(pageNum, pageSize);
		Page<PaymentFlowDetailDTO> list = detailDao.selectPaymentFlow(conditionDTO);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("data",list);
		map.put("total",list.getTotal());
		PaymentFlowDetailDTO dto = detailDao.selectPaymentFlowAmountStatic(conditionDTO);
		List<PaymentFlowDetailDTO> staticList=new ArrayList<PaymentFlowDetailDTO>();
		staticList.add(dto);
		map.put("static",staticList);
		return map;
	}


	@Override
	public HSSFWorkbook downLoad(PaymentFlowConditionDTO conditionDTO) throws MarvelsBusinessException {
		HSSFWorkbook wb = new HSSFWorkbook();
		List<PaymentFlowDetailDTO> list=detailDao.downPaymentFlow(conditionDTO);
		HSSFSheet sheet = wb.createSheet("还款流水详情数据一览表");
		HSSFRow row = sheet.createRow((int) 0);
		sheet.setDefaultColumnWidth(10);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		// 居中格式、加粗
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 11);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setBorderBottom((short) 1);// 下边框
		style.setBorderLeft((short) 1);// 左边框
		style.setBorderRight((short) 1);// 右边框
		style.setBorderTop((short) 1);// 上边框
		style.setLocked(true);
		style.setFont(font);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("进件号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("身份证");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("电话号码");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("产品");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("发起交易时间");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("发起交易金额");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("实际交易金额");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("还款方式");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("银行名称");
		cell.setCellStyle(style);
		cell = row.createCell(11);
		cell.setCellValue("银行卡号");
		cell.setCellStyle(style);

		cell = row.createCell(12);
		cell.setCellValue("产品渠道");
		cell.setCellStyle(style);
		cell = row.createCell(13);
		cell.setCellValue("交易结果");
		cell.setCellStyle(style);
		cell = row.createCell(14);
		cell.setCellValue("返回信息");
		cell.setCellStyle(style);
		cell = row.createCell(15);
		cell.setCellValue("订单号");
		cell.setCellStyle(style);
		if (null!=list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				// 创建单元格，并设置值
				row.createCell(0).setCellValue(i+1);
				row.createCell(1).setCellValue(list.get(i).getLoanId());
				row.createCell(2).setCellValue(list.get(i).getCustName());
				row.createCell(3).setCellValue(list.get(i).getCustCardNo());
				row.createCell(4).setCellValue(list.get(i).getMobileNum());
				row.createCell(5).setCellValue(list.get(i).getProductTypeName());
				row.createCell(6).setCellValue(DateUtils.getDateStr(list.get(i).getTradeTime(), "yyyy-MM-dd HH:mm:ss"));
				row.createCell(7).setCellValue(list.get(i).getPaymentAmount()+"");
				row.createCell(8).setCellValue(list.get(i).getPaymentAmountAct()+"");
				if("1".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("自动代扣");
				}else if("2".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("主动还款");
				}else if("3".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("提前结清");
				}else if("4".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("主动还款");
				}else if("5".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("下期还款");
				}else if("6".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("手动代扣");
				}else if("9".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("支付宝还款");
				}else if("10".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("支付宝提前结清");
				}else if("11".equals(list.get(i).getPaymentType())){
					row.createCell(9).setCellValue("展期扣费");
				}
				row.createCell(10).setCellValue(list.get(i).getBankName());
				row.createCell(11).setCellValue(list.get(i).getBankCard());
				
				row.createCell(12).setCellValue(list.get(i).getChannelName());
				if("1".equals(list.get(i).getPaymentResult())){
					row.createCell(13).setCellValue("交易中");
				}else if("2".equals(list.get(i).getPaymentResult()) || "3".equals(list.get(i).getPaymentResult())|| "4".equals(list.get(i).getPaymentResult())){
					row.createCell(13).setCellValue("代扣成功");
				}else if("9".equals(list.get(i).getPaymentResult())){
					row.createCell(13).setCellValue("代扣失败");
				}
				row.createCell(14).setCellValue(list.get(i).getReturnMsg());
				row.createCell(15).setCellValue(list.get(i).getOrderNumber());
			}
		}
		
		return wb;
	}

	@Override
	public MarvelsResultDTO trialCostList(TrialCostConditionDTO conditionDTO) throws MarvelsBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		TrialCostDataDTO dto=consumerCoreApi.countData(conditionDTO);//统计列表条数
		if(dto!=null){
			map.put("total", dto.getTotal());
			map.put("totalAmount", dto.getTotalAmount());
		}
		Integer pageNum=conditionDTO.getPageNum()==null?PageConstant.CURRPAGE:conditionDTO.getPageNum();
		Integer pageSize=conditionDTO.getPageSize()==null?PageConstant.PAGESIZE:conditionDTO.getPageSize();
		conditionDTO.setLimit((pageNum-1)*pageSize);
		conditionDTO.setOffet(pageSize);
		List<TrialCostDataDTO> list=consumerCoreApi.listQuery(conditionDTO);//分页查询列表
		map.put("list", list);
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), map);
	}

	@Override
	public HSSFWorkbook trialCostDownLoad(TrialCostConditionDTO conditionDTO) throws MarvelsBusinessException {
		conditionDTO.setIsDownLoad("1");//下载
		List<TrialCostDataDTO> list=consumerCoreApi.listQuery(conditionDTO);
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("信贷审批费用明细");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);

		cell = row.createCell((short) 1);
		cell.setCellValue("客户名称");
		cell.setCellStyle(style);
		
		cell = row.createCell((short) 2);
		cell.setCellValue("客户身份证");
		cell.setCellStyle(style);
		cell = row.createCell((short) 3);
		cell.setCellValue("客户手机");
		cell.setCellStyle(style);
		cell = row.createCell((short) 4);
		cell.setCellValue("银行卡号");
		cell.setCellStyle(style);
		cell = row.createCell((short) 5);
		cell.setCellValue("开户行名称");
		cell.setCellStyle(style);
		cell = row.createCell((short) 6);
		cell.setCellValue("产品");
		cell.setCellStyle(style);
		cell = row.createCell((short) 7);
		cell.setCellValue("产品期数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 8);
		cell.setCellValue("渠道");
		cell.setCellStyle(style);
		cell = row.createCell((short) 9);
		cell.setCellValue("扣款订单号");
		cell.setCellStyle(style);
		
		cell = row.createCell((short) 10);
		cell.setCellValue("扣款日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 11);
		cell.setCellValue("收取金额");
		cell.setCellStyle(style);
		cell = row.createCell((short) 12);
		cell.setCellValue("状态");
		cell.setCellStyle(style);
		cell = row.createCell((short) 13);
		cell.setCellValue("详细结果");
		cell.setCellStyle(style);
		
		// 第五步，写入实体数据 实际应用中这些数据从数据库得到，
		if(list==null || list.size()<1){
			return wb;
		}
		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) i + 1);
			// 第四步，创建单元格，并设置值
			cell = row.createCell((short) 0);
			cell.setCellValue(i + 1);
			
			cell = row.createCell((short) 1);
			cell.setCellValue(list.get(i).getCustName());
			
			cell = row.createCell((short) 2);
			cell.setCellValue(list.get(i).getCustCardNo());
		    cell = row.createCell((short) 3);
			cell.setCellValue(list.get(i).getMobileNum());
			cell = row.createCell((short) 4);
			cell.setCellValue(list.get(i).getPaymentBankCard());
			cell = row.createCell((short) 5);
			cell.setCellValue(list.get(i).getPaymentBankName());
			cell = row.createCell((short) 6);
			cell.setCellValue(list.get(i).getProductTypeName());
			
			cell = row.createCell((short) 7);
			cell.setCellValue(list.get(i).getApplyTerms());

			cell = row.createCell((short) 8);
			cell.setCellValue(list.get(i).getChannelType());
			cell = row.createCell((short) 9);
			cell.setCellValue(list.get(i).getOrderNumber());
			cell = row.createCell((short) 10);
			cell.setCellValue(list.get(i).getUpdatedDate());
			cell = row.createCell((short) 11);
			Double amount = list.get(i).getAmount().doubleValue() / 100;
			cell.setCellValue(amount+"" );
			cell = row.createCell((short) 12);
			String state=list.get(i).getState();
			if ("0".equals(state) ){
				cell.setCellValue("扣款中");
			}else if ("1".equals(state)) {
				cell.setCellValue("已扣款");
			}else if ("2".equals(state)) {
				cell.setCellValue("取消扣款");
			}else if ("3".equals(state)) {
				cell.setCellValue("扣款失败");
			}
			cell = row.createCell((short) 13);
			cell.setCellValue(list.get(i).getRetMsg());
		}	
		return wb;
	}

	@Override
	public HSSFWorkbook trialCostCollectDownLoad(TrialCostConditionDTO conditionDTO) throws MarvelsBusinessException {
		List<TrialCostDataDTO> list=consumerCoreApi.costCollect(conditionDTO);
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("征审费收入汇总");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);

		cell = row.createCell((short) 1);
		cell.setCellValue("所属部门");
		cell.setCellStyle(style);
		
		cell = row.createCell((short) 2);
		cell.setCellValue("资金方");
		cell.setCellStyle(style);
		cell = row.createCell((short)3);
		cell.setCellValue("产品");
		cell.setCellStyle(style);
		cell = row.createCell((short) 4);
		cell.setCellValue("期数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 5);
		cell.setCellValue("笔数");
		cell.setCellStyle(style);
		
		cell = row.createCell((short) 6);
		cell.setCellValue("咨询费用金额合计");
		cell.setCellStyle(style);
		int totalCount=0;
		Double totalFee=0d;

		if(list!=null && list.size()>1){
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				// 第四步，创建单元格，并设置值
				cell = row.createCell((short) 0);
				cell.setCellValue(i + 1);
				
				cell = row.createCell((short) 1);
				cell.setCellValue("科技金融事业部");
				
				cell = row.createCell((short) 2);
				cell.setCellValue(list.get(i).getFundName());
			    cell = row.createCell((short) 3);
				cell.setCellValue(list.get(i).getProductTypeName());
				cell = row.createCell((short) 4);
				cell.setCellValue(list.get(i).getTerms());
				cell = row.createCell((short) 5);
				cell.setCellValue(list.get(i).getCount());
				cell = row.createCell((short) 6);
				Double amount =list.get(i).getAmount().doubleValue()/ 100;
				cell.setCellValue(amount+"");
				totalCount=totalCount+list.get(i).getCount();
				totalFee=totalFee+list.get(i).getAmount().doubleValue()/100;
			}
		}
		row = sheet.createRow((list==null?0:list.size())+4 );
		cell = row.createCell((short) 1);
		cell.setCellValue("合计");
		cell = row.createCell((short) 2);
		cell.setCellValue("");
	    cell = row.createCell((short) 3);
		cell.setCellValue("");
		cell = row.createCell((short) 4);
		cell.setCellValue("");
		cell = row.createCell((short) 5);
		cell.setCellValue(totalCount);
		cell = row.createCell((short) 6);
		cell.setCellValue(totalFee);
		return wb;
	}

}
